﻿//选择客户(多选）
var columns =
            [
                 { display: 'ProductSellBillDetailID', name: 'ProductSellBillDetailID', type: 'text', hide: 1 },
                  { display: 'StockID', name: 'StockID', type: 'text', hide: 1 },
                  { display: 'ProductID', name: 'ProductID', type: 'text', hide: 1 },
                  { display: '产品类别', name: 'TypeName', mintWidth: 80, width: 120 },
                { display: '产品名称', name: 'ProductName', mintWidth: 80, width: 120 },
                { display: '产品编码', name: 'ProductCode', mintWidth: 100, width: 140 },
                { display: '产品规格', name: 'ProductStandard', mintWidth: 100, width: 140 },
                 { display: '销售单号', name: 'ProductSellBillo', mintWidth: 100, width: 140 },
                  { display: '销售时间', name: 'SellTime', mintWidth: 100, width: 140, type: 'selfDate' },
                   { display: '销售数量', name: 'SellAmount', mintWidth: 100, width: 140 },
                    { display: '销售单价', name: 'SellPrice', mintWidth: 100, width: 140 },
                { display: '所属仓库', name: 'StorehouseName', mintWidth: 80, width: 120 }
             ];

var manager;
$(document).ready(function () {
    initligerGrid();
    $("#btnSearch").unbind("click").bind("click", initligerGrid);
    $("#submitBtn").unbind("click").bind("click", selectOperation);
});

function initligerGrid() {
    var wh = $(window).height();
    //var fh = $("#" + formDiv).height();
    var gridHeight = 438;//  wh - fh - 50;
    manager = null;
    manager = $("#productsGrid").ligerGrid({
        columns: columns,
        title: '产品信息列表',
        showTitle: true,
        width: '100%',
        enabledSort: true,
        height: gridHeight,
        checkbox: true,
        rownumbers: true,
        isChecked: f_isChecked, onCheckRow: f_onCheckRow, onCheckAllRow: f_onCheckAllRow,
        parms: { orgCode: $("#HidOrgCode").val(), clientCode: $("#HidClientId").val(), productGG: $("#productGG").val(), productName: $("#productName").val(), productCode: $("#productCode").val() },
        url: "../ProductDelivery/GetJsonOfSellProducts?orgCode=" + $("#HidOrgCode").val()
    });
}




//update 
function selectOperation() {

    var strHtml = new Array([checkedCustomer.length]);
    var data = manager.rows;
    var len = data.length;
    for (var i = 0; i < len; i++) {
        for (var j = 0; j < checkedCustomer.length; j++) {
            if (data[i]["ProductSellBillDetailID"] == checkedCustomer[j]) {
                strHtml[j] = checkedCustomer[j] + "," + data[i]["ProductName"] + "," + data[i]["StockID"] + "," + data[i]["ProductID"] + "," + data[i]["TypeName"] + "," + data[i]["ProductCode"] + "," + data[i]["ProductStandard"];
            }
        }
    }
    window.returnValue = strHtml;
    window.close();
}

//=========================================
function f_onCheckAllRow(checked) {
    for (var rowid in this.records) {
        if (checked)
            addCheckedCustomer(this.records[rowid]['ProductSellBillDetailID']);
        else
            removeCheckedCustomer(this.records[rowid]['ProductSellBillDetailID']);
    }
}

/*
该例子实现 表单分页多选
即利用onCheckRow将选中的行记忆下来，并利用isChecked将记忆下来的行初始化选中
*/
var checkedCustomer = [];
function findCheckedCustomer(CustomerID) {
    for (var i = 0; i < checkedCustomer.length; i++) {
        if (checkedCustomer[i] == CustomerID) return i;
    }
    return -1;
}
function addCheckedCustomer(CustomerID) {
    if (findCheckedCustomer(CustomerID) == -1)
        checkedCustomer.push(CustomerID);
}
function removeCheckedCustomer(CustomerID) {
    var i = findCheckedCustomer(CustomerID);
    if (i == -1) return;
    checkedCustomer.splice(i, 1);
}
function f_isChecked(rowdata) {
    if (findCheckedCustomer(rowdata.ProductSellBillDetailID) == -1)
        return false;
    return true;
}
function f_onCheckRow(checked, data) {
    if (checked) addCheckedCustomer(data.ProductSellBillDetailID);
    else removeCheckedCustomer(data.ProductSellBillDetailID);
}
function f_getChecked() {
    alert(checkedCustomer.join(','));
}
